package org.hcyspartnoc.luoxin.controller.asset;

import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import org.hcyspartnoc.luoxin.common.core.AjaxResult;
import org.hcyspartnoc.luoxin.common.core.PageBean;
import org.hcyspartnoc.luoxin.common.core.annotation.Log;
import org.hcyspartnoc.luoxin.common.core.annotation.PreAuthorize;
import org.hcyspartnoc.luoxin.common.core.enums.BusinessType;
import org.hcyspartnoc.luoxin.common.exception.asset.AssetVCounselorException;
import org.hcyspartnoc.luoxin.common.exception.common.CommonUtilsException;
import org.hcyspartnoc.luoxin.common.exception.framework.AuthException;
import org.hcyspartnoc.luoxin.pojo.dto.asset.AssetVCounselorAddDto;
import org.hcyspartnoc.luoxin.pojo.dto.asset.AssetVCounselorModifyDto;
import org.hcyspartnoc.luoxin.pojo.po.asset.AssetVirtualCounselorPo;
import org.hcyspartnoc.luoxin.pojo.vo.asset.AssetVCounselorAdminVo;
import org.hcyspartnoc.luoxin.pojo.vo.asset.AssetVCounselorVo;
import org.hcyspartnoc.luoxin.service.asset.IAssetVirtualCounselorService;
import org.hibernate.validator.constraints.Length;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/asset/vCounselor")
public class AssetVirtualCounselorController {
    @Resource
    private IAssetVirtualCounselorService assetVirtualCounselorService;

    @Log(title = "资产·虚拟咨询师·新增", businessType = BusinessType.INSERT)
    @PreAuthorize(auth = "asset:vCounselor:add", descr = "新增虚拟咨询师")
    @PostMapping("/add")
    public AjaxResult addVirtualCounselor(@Valid @RequestBody AssetVCounselorAddDto assetVCounselorAddDto) throws CommonUtilsException, AssetVCounselorException {
        assetVirtualCounselorService.addVirtualCounselor(assetVCounselorAddDto);
        return AjaxResult.successMsg();
    }

    @Log(title = "资产·虚拟咨询师·修改", businessType = BusinessType.UPDATE)
    @PreAuthorize(auth = "asset:vCounselor:modify", descr = "编辑虚拟咨询师")
    @PutMapping("/modify")
    public AjaxResult modifyVirtualCounselor(@Valid @RequestBody AssetVCounselorModifyDto assetVCounselorModifyDto) throws AssetVCounselorException, CommonUtilsException, AuthException {
        assetVirtualCounselorService.modifyVirtualCounselor(assetVCounselorModifyDto);
        return AjaxResult.successMsg();
    }

    @Log(title = "资产·虚拟咨询师·查询·分页", businessType = BusinessType.OTHER)
    @PreAuthorize(auth = "asset:vCounselor:query:page", descr = "分页查询虚拟咨询师")
    @GetMapping("/query/page")
    public AjaxResult queryByPage(@Min(value = 1, message = "当前页不能小于1")
                                  @RequestParam(defaultValue = "1") int currentPage,
                                  @Min(value = PageBean.MIN_PAGE_SIZE, message = "每页大小" + PageBean.MIN_PAGE_SIZE + "-" + PageBean.MAX_PAGE_SIZE)
                                  @Max(value = PageBean.MAX_PAGE_SIZE, message = "每页大小" + PageBean.MIN_PAGE_SIZE + "-" + PageBean.MAX_PAGE_SIZE)
                                  @RequestParam(defaultValue = "5") int pageSize,
                                  @Length(max = 10, message = "名称长度0-10")
                                  @RequestParam(defaultValue = "") String name,
                                  @Length(max = 255, message = "疗法最长255")
                                  @RequestParam(defaultValue = "") String therapy,
                                  @Length(max = 255, message = "风格最长255")
                                  @RequestParam(defaultValue = "") String styleStringList,
                                  @Min(value = 0, message = "开放状态值0-2")
                                  @Max(value = 2, message = "开放状态值0-2")
                                  @RequestParam(defaultValue = "0") Integer isOpen) {
        PageBean<AssetVirtualCounselorPo, AssetVCounselorAdminVo> pageBean = assetVirtualCounselorService.queryByPage(currentPage, pageSize, name, therapy, styleStringList,isOpen);
        return AjaxResult.success(pageBean);
    }

    @Log(title = "资产·虚拟咨询师·删除", businessType = BusinessType.DELETE)
    @PreAuthorize(auth = "asset:vCounselor:remove:id", descr = "删除虚拟咨询师")
    @DeleteMapping("/remove/{vCounselorId:\\d{1,10}}")
    public AjaxResult removeById(@PathVariable Integer vCounselorId) throws AssetVCounselorException, AuthException {
        assetVirtualCounselorService.removeByVCounselorId(vCounselorId);
        return AjaxResult.successMsg();
    }

    @Log(title = "资产·虚拟咨询师·修改·开放状态", businessType = BusinessType.UPDATE)
    @PreAuthorize(auth = "asset:vCounselor:modify:isOpen", descr = "修改虚拟咨询师开放状态")
    @PutMapping("/modify/isOpen/{vCounselorId:\\d{1,10}}")
    public AjaxResult modifyIsOpen(@PathVariable Integer vCounselorId) throws AuthException, AssetVCounselorException {
        assetVirtualCounselorService.modifyIsOpen(vCounselorId);
        return AjaxResult.successMsg();
    }

    @Log(title = "资产·虚拟咨询师·查询·通过token获取", businessType = BusinessType.OTHER)
    @PreAuthorize(auth = "asset:vCounselor:query:token", descr = "通过token获取用户所拥有的虚拟咨询师")
    @GetMapping("/query/page/token")
    public AjaxResult queryByToken(@Min(value = 1, message = "当前页不能小于1")
                                   @RequestParam(defaultValue = "1") int currentPage,
                                   @Min(value = PageBean.MIN_PAGE_SIZE, message = "每页大小" + PageBean.MIN_PAGE_SIZE + "-" + PageBean.MAX_PAGE_SIZE)
                                   @Max(value = PageBean.MAX_PAGE_SIZE, message = "每页大小" + PageBean.MIN_PAGE_SIZE + "-" + PageBean.MAX_PAGE_SIZE)
                                   @RequestParam(defaultValue = "5") int pageSize) {
        PageBean<AssetVirtualCounselorPo, AssetVCounselorVo> pageBean = assetVirtualCounselorService.queryByToken(currentPage, pageSize);
        return AjaxResult.success(pageBean);
    }


}
